package com.zsk.shop.controller.app;

import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.utils.StringUtils;
import com.zsk.shop.enums.EnumOrderStatus;
import com.zsk.shop.model.params.ConfirmOrderParams;
import com.zsk.shop.model.params.GetOrderParam;
import com.zsk.shop.model.params.ShopOrderParam;
import com.zsk.shop.model.results.ConfirmOrderResult;
import com.zsk.shop.model.results.ShopOrderInfoResult;
import com.zsk.shop.model.results.ShopOrderListResult;
import com.zsk.shop.service.ShopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * @author Ys
 * @date 2022年01月22日 15:23
 */
@RestController
@Api(tags = "商城-订单/支付相关")
@RequestMapping("/shop/order")
public class ShopOrderController {

    private static final Logger logger = LoggerFactory.getLogger(ShopOrderController.class);

    @Autowired
    private ShopOrderService orderService;

    @PostMapping(value = "/getOrderList.do")
    @ApiOperation("Shop->>订单列表")
    public BaseResponse<ShopOrderListResult> getOrderList(@RequestBody GetOrderParam param) {
        return orderService.getOrderList(param);
    }

    @GetMapping(value = "/getOrderInfo.do")
    @ApiOperation("Shop->>订单详情")
    @ApiImplicitParam(name = "orderNo", value = "订单编号")
    public BaseResponse<ShopOrderInfoResult> getOrderInfo(String orderNo) {
        return orderService.getOrderInfo(orderNo);
    }

    @GetMapping(value = "/cancelOrder.do")
    @ApiOperation("Shop->>取消订单")
    @ApiImplicitParam(name = "orderNo", value = "订单编号")
    public BaseResponse cancelOrder(String orderNo) {

        return orderService.updateOrderEndStatus(orderNo, EnumOrderStatus.CANCEL_ORDER.getCode());
    }

    @GetMapping(value = "/confirmOrder.do")
    @ApiOperation("Shop->>确认收货")
    @ApiImplicitParam(name = "orderNo", value = "订单编号")
    public BaseResponse confirmOrder(String orderNo) {
        return orderService.updateOrderEndStatus(orderNo, EnumOrderStatus.COMPLETE.getCode());
    }

    @PostMapping(value = "/queryOrderInfo.do")
    @ApiOperation("Shop->>确认订单页")
    public BaseResponse<ConfirmOrderResult> getOrderInfo(@RequestBody ConfirmOrderParams param) {
        return orderService.getOrderInfo(param);
    }

    @PostMapping(value = "/create.do")
    @ApiOperation("下单")
    public BaseResponse create(@RequestBody @Validated ShopOrderParam param) throws Exception {
        return orderService.add(param);
    }

    @GetMapping(value = "/pay.do")
    @ApiOperation("待支付-支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单列表ID"),
            @ApiImplicitParam(name = "payType", value = "支付方式")})
    public BaseResponse pay(Long id, Integer payType) throws Exception {
        return orderService.orderPay(id, payType);
    }

    @GetMapping(value = "/aliPcPay.do")
    @ApiOperation("支付宝H5待支付-支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单列表ID"),
            @ApiImplicitParam(name = "payType", value = "支付方式")})
    public void aliPcPay(HttpServletRequest request, HttpServletResponse response, Long id, Integer payType) throws Exception {
        orderService.aliPcPay(request, response, id, payType);
    }

    /**
     * PC端支付创建订单(支付宝)
     * @param request
     * @param response
     * @throws Exception
     */
    @PostMapping(value = "/createPcOrder.do")
    @ApiOperation("PC支付创建订单")
    public void createPcOrder(HttpServletRequest request, HttpServletResponse response, @RequestBody @Validated ShopOrderParam param) throws Exception {
        orderService.createPcOrder(request, response, param);
    }

    @GetMapping(value = "/deleteOrder.do")
    @ApiOperation("删除订单")
    @ApiImplicitParam(name = "id", value = "订单列表ID")
    public BaseResponse deleteOrder(Long id){
        return orderService.deleteOrder(id);
    }

}
